<?php

namespace app\api\controller;
use app\api\dao\QuestionDao;
use app\Request;
use think\facade\Db;

//答题
class Question extends Api
{
    public $class=[
        "5ra42ugg80"=>10, //优选驾考
    ];

    //题库分类
    public function class(Request $request)
    {
        $class=isset($this->class[$this->appid]) ? $this->class[$this->appid]: 10;
        $return =  (new QuestionDao())->cate($class);
        $this->success($return);
    }

    //章节分类
    public function chapter(Request $request)
    {
        $cate=$request->param("cate");
        $label=$request->param("label");
        if(empty($cate))  $this->error("缺少参数！");
        $class=isset($this->class[$this->appid]) ? $this->class[$this->appid]: 10;
        $return =  (new QuestionDao())->chapter($class,$cate,$label);
        $this->success($return);
    }

    //标签分类
    public function label(Request $request)
    {
        $cate=$request->param("cate");
        $label=$request->param("label");
        if(empty($cate))  $this->error("缺少参数！");
        $class=isset($this->class[$this->appid]) ? $this->class[$this->appid]: 10;
        $return =  (new QuestionDao())->label($class,$cate,$label);
        $this->success($return);
    }


    //图标分类
    public function group(Request $request)
    {
        $class=isset($this->class[$this->appid]) ? $this->class[$this->appid]: 10;
        $return =  (new QuestionDao())->group($class);
        $this->success($return);
    }
    //图标列表
    public function image(Request $request)
    {
        $group_id=$request->param("group_id");
        if(empty($group_id))  $this->error("缺少参数！");
        $class=isset($this->class[$this->appid]) ? $this->class[$this->appid]: 10;
        $return =  (new QuestionDao())->images($class,$group_id);
        $this->success($return);
    }

    //所有题库ID
    public function all(Request $request)
    {
        $where=$request->param();
        if(empty($where["cate"]))  $this->error("题目分类出错！");
        //题库鉴权映射
        $class=isset($this->class[$this->appid]) ? $this->class[$this->appid] :10;
        //是否会员判断
        $this->is_vip($request);
        $return =  (new QuestionDao())->all($where,$class);
        $ids=array_column($return,"id");
        $this->success($ids);
    }

    //题目列表详情
    public function list(Request $request)
    {
        $ids=$request->param("ids");
        $vip=$request->param("vip",10);
        //题库鉴权映射
        $class=isset($this->class[$this->appid]) ? $this->class[$this->appid] :10;
        //是否会员判断
        $this->is_vip($request);
        if(empty($ids)) $this->error("题目ID出错！");
        if(count(explode(",",$ids)) > 20) $this->error("题目ID数量出错！");
        $return = (new QuestionDao())->list($ids,$vip,$class);
        $this->success($return);
    }



    /*············································*/
    /*验证会员机制*/
    public function is_vip($request)
    {
        $vip=$request->param("vip");
        //是否会员判断
        if($vip)
        {
            if(empty($this->userInfo)) $this->error("您还不是会员！");
            if($this->userInfo["vip"]!==20) $this->error("您还不是会员！");
        }
    }







}